hdu 1199 Color the Ball 离散线段树

C - Color the Ball
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

There are infinite balls in a line (numbered 1 2 3 ....), and initially all of them are paint black. Now Jim use a brush paint the balls, every time give two integers a b and follow by a char 'w' or 'b', 'w' denotes the ball from a to b are painted white, 'b' denotes that be painted black. You are ask to find the longest white ball sequence.
 

Input

First line is an integer N (<=2000), the times Jim paint, next N line contain a b c, c can be 'w' and 'b'.

There are multiple cases, process to the end of file.
 

Output

Two integers the left end of the longest white ball sequence and the right end of longest white ball sequence (If more than one output the small number one). All the input are less than 2^31-1. If no such sequence exists, output "Oh, my god".
 

Sample Input

3 1 4 w 8 11 w 3 5 b
 

Sample Output

8 11
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)

const int inf=0x7fffffff;   //无限大
char op[3];
struct interval
{
    int start,endn;
    bool operator < (const interval& b)const{
        if(start!=b.start)
            return start<b.start;
        else
            return endn<b.endn;
    }
};
interval val[20001];
void white(int a,int b,int& cnt)
{
    val[cnt].start=a;
    val[cnt].endn=b;
    cnt++;
}
void black(int a,int b,int& cnt)
{
    int tmp=cnt;
    for(int i=0;i<cnt;i++)
    {
        if(val[i].start<a)
        {
            if(val[i].endn>=a)
            {
                if(val[i].endn<=b)
                {
                    val[i].endn=a-1;
                }
                else
                {
                    val[tmp].start=b+1;
                    val[tmp].endn=val[i].endn;
                    tmp++;
                    val[i].endn=a-1;
                }
            }
        }
        else if(val[i].start<=b)
        {
            if(val[i].endn<=b)
            {
                val[i].start=0;
                val[i].endn=-1;
            }
            else
            {
                val[i].start=b+1;
            }
        }
    }
    cnt=tmp;
}
int solve(int cnt,int& index)
{
    sort(val,val+cnt);
    int maxn=val[0].endn-val[0].start+1;
    for(int i=1;i<cnt;i++)
    {
        if(val[i].start!=0)
        {
            if(val[i].start<=val[i-1].endn+1)
            {
                if(val[i-1].endn<=val[i].endn)
                {
                    val[i].start=val[i-1].start;
                }
                else
                {
                    val[i].start=val[i-1].start;
                    val[i].endn=val[i-1].endn;
                }
            }
            if(val[i].endn-val[i].start+1>maxn)
            {
                maxn=val[i].endn-val[i].start+1;
                index=i;
            }
        }
    }
    return maxn;
}
int main()
{
    int n,index,a,b,c;
    while(cin>>n)
    {
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%s",&a,&b,op);
            if(a>b)
            {
                swap(a,b);
            }
            if(op[0]=='w')
                white(a,b,cnt);
            else
                black(a,b,cnt);
        }
        index=0;
        if(solve(cnt,index))
        {
            cout<<val[index].start<<" "<<val[index].endn<<endl;
        }
        else
            cout<<"Oh, my god"<<endl;
    }
    return 0;
}

 

 
posted @ 2015-01-24 12:40  qscqesze  阅读(412)  评论(0编辑  收藏  举报